/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Operations.Algebra;
import Operations.*;
import java.awt.image.BufferedImage;
/**
 *
 * @author littlechild
 */
public class OpShiftMinus implements Operation {
    /**
     * @param image
     * @param paras
     *  paras[0] is the distance to move
     * @return
     */
    public BufferedImage operate(BufferedImage image, double[] paras) {
       int distance=(int)paras[0];
       BufferedImage returnImage=new BufferedImage(image.getWidth(),image.getHeight(),image.getType());
       int rgb1,rgb2;
       int rgbmask=0xffffff;
       if(distance<=0) {
           for(int i=0;i!=image.getWidth()+distance;++i) {
               for(int j=0;j!=image.getHeight();++j) {
                   //returnImage.setRGB(i, j, (image.getRGB(i, j)&rgbmask)-(image.getRGB(i-distance,j)&rgbmask));
                   rgb1=(image.getRGB(i, j))&rgbmask;
                   rgb2=(image.getRGB(i-distance,j)&rgbmask);
                   //int red=(255+((rgb>>16)&0xff)-((rgb2>>16)&0xff))/2;
                   //int green=(255+((rgb>>8)&0xff)-((rgb2>>8)&0xff))/2;
                   //int blue=(255+(rgb&0xff)-(rgb2&0xff))/2;
                    int red=((rgb1>>>16)&0xff)-((rgb2>>>16)&0xff);
                    red=(red >0 ) ? red : 0;
                    int green=((rgb1>>>8)&0xff)-((rgb2>>>8)&0xff);
                    green=(green >0 ) ? green :0;
                    int blue=((rgb1)&0xff)-((rgb2)&0xff);
                    blue=(blue>0) ? blue : 0;
                   returnImage.setRGB(i, j,(red<<16)|(green<<8)|blue);
               }
           }
           for(int i=image.getWidth()+distance;i!=image.getWidth();++i) {
               for(int j=0;j!=image.getHeight();++j) {
                   returnImage.setRGB(i,j,image.getRGB(i, j));
               }
           }
       }else if (distance>0){
           for(int i=0;i!=distance;++i) {
              for(int j=0;j!=image.getHeight();++j) {
                   returnImage.setRGB(i,j,image.getRGB(i, j));
               }
           }
           for(int i=distance;i!=image.getWidth();++i) {
               for(int j=0;j!=image.getHeight();++j) {
                   rgb1=(image.getRGB(i, j))&rgbmask;
                   rgb2=(image.getRGB(i-distance,j)&rgbmask);
                    int red=((rgb1>>>16)&0xff)-((rgb2>>>16)&0xff);
                    red=(red >0 ) ? red : 0;
                    int green=((rgb1>>>8)&0xff)-((rgb2>>>8)&0xff);
                    green=(green >0 ) ? green :0;
                    int blue=((rgb1)&0xff)-((rgb2)&0xff);
                    blue=(blue>0) ? blue : 0;
                   returnImage.setRGB(i, j,(red<<16)+(green<<8)+blue);
               }
           }
       }
       return returnImage;
    }

}
